package com.ruoyi.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.AnonymousAccess;
import com.ruoyi.common.config.PayBaseInfo;
import com.ruoyi.common.utils.pay.Md5Util;
import com.ruoyi.common.vo.PayRes;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.domain.vo.NotifyResult;
import com.ruoyi.system.domain.vo.PaySubmitVo;
import com.ruoyi.system.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/pay")
@Api(tags="支付")
public class PayController {

    @Autowired
    private IPayService payService;

    @PostMapping("/submit")
    @ApiOperation(value="支付-提交支付请求", notes="支付-提交支付请求")
    @AnonymousAccess
    public Result<?> submit(@RequestBody PaySubmitVo paySubmitVo, HttpServletRequest request) throws Exception {
        return Result.OK(payService.paySubmit(paySubmitVo, request));
    }

    @GetMapping("/notifyUrl")
    @AnonymousAccess
    public String notifyUrl(NotifyResult notifyResult) {
        return payService.payNotify(notifyResult);
    }

    public static void main(String[] args) {
        String orderId = "1676088238039625730";
        String clientip = "127.0.0.1";
        String money = "0.01";
        String name = "tai deng 1";
        String device = "pc";
        String type = "wxpay";
//        String pid = "1000";
//        String pid = "1660";
        String pid = "2446";
        String notify_url = "http://562d2b20.r7.cpolar.top/pay/notifyUrl";
        String return_url = "http://localhost:8080/pay/return";
        //API接口支付
//        String url = "https://www.alywlzf.com/mapi.php";
        String url = "https://api.payqqpay.cn/mapi.php";
//        String url = "https://y.aliyunzf.cn/mapi.php";
        //pid={商户ID}&type={支付方式}&out_trade_no={商户订单号}&notify_url={服务器异步通知地址}&return_url={页面跳转通知地址}&name={商品名称}&money={金额}&clientip={用户IP地址}&device={设备类型}&sign={签名字符串}&sign_type=MD5
        String out_trade_no = orderId, sign = "";
        String sign_type = "MD5";
//        String key = "WBZHZWBeheKhWZcKuGRlb8lKWzwCWUeH";
//        String key = "yJbvcjd7AgsFjdOmKiiT1vn2yMpl1BU0";
        String key = "8XMXgMxh88XXFz87zxmHdtGDGQgjxHAr";
        //[clientip, device, money, name, notify_url, out_trade_no, pid, return_url, sign_type, type]
        //封装好我们的请求参数
        String mdString = "clientip=" + clientip + "&device=" + device + "&money=" + money + "&name=" + name + "&" +
                "notify_url=" + notify_url + "&out_trade_no=" + out_trade_no + "&pid=" + pid + "&return_url=" + return_url +
                "&type=" + type + key;

        //把我们的请求参数 md5加密
        sign = Md5Util.GetMD5Code(mdString);

        //封装我们的请求参数 调用的api  是我们
        Map<String, Object> map = new HashMap<>();
        map.put("clientip", clientip);
        map.put("device", device);
        map.put("money", money);
        map.put("name", name);
        map.put("notify_url", notify_url);
        map.put("out_trade_no", out_trade_no);
        map.put("pid", pid);
        map.put("return_url", return_url);
        map.put("sign_type", sign_type);
        map.put("type", type);
        map.put("sign", sign);
        String body = HttpUtil.post(url, map);
        PayRes payRes = JSON.toJavaObject(JSONObject.parseObject(body), PayRes.class);

    }
}
